package com.zyw.customcalendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.util.ArrayMap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.zyw.customcalendar.common.util.CharacterParser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import de.greenrobot.event.EventBus;

public class CityMianActivity extends Activity {

    private ListView mCitylist;
    private ImageView cacelBtn;
    private LinearLayout mCitySortLayout;

    private ArrayList<String> mCityStrings;
    private ArrayMap<String, List<String>> mCityDatas;
    private String selectCityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_mian);

        selectCityName = getIntent().getStringExtra("city_name");

        mCitylist = (ListView) findViewById(R.id.city_list);

        mCityStrings = new ArrayList<String>();
        mCityDatas = new ArrayMap<String, List<String>>();

        String[] allCity=getResources().getStringArray(R.array.all_city);
        updateCityDatas(allCity);

        String[] hotCity=getResources().getStringArray(R.array.hot_city);
        List<String> hotlist= Arrays.asList(hotCity);
        mCityDatas.put("热门城市", hotlist);
        mCityStrings.add(0, "热门城市");

        mCitylist.setAdapter(new CityAdapter());
        mCitylist.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(mCitySortLayout != null) {
                    updateSelectSortView(mCitySortLayout.getChildAt(firstVisibleItem - 1));
                }
            }
        });

        cacelBtn = (ImageView) findViewById(R.id.cacel_btn);

        cacelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(0);
                finish();
            }
        });

        mCitySortLayout = (LinearLayout) findViewById(R.id.city_sort_layout);
        initCitySortLayout();
    }

    private TextView selectView;

    private void initCitySortLayout(){
        for(int i = 1; i < mCityStrings.size(); i++){
            TextView textView = new TextView(CityMianActivity.this);
            textView.setText(mCityStrings.get(i));
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.parseColor("#999999"));
            textView.setTag(i);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = (Integer) v.getTag();
                    mCitylist.setSelection(index);
                    updateSelectSortView(v);
                }
            });
            mCitySortLayout.addView(textView);
        }
    }

    private void updateSelectSortView(View v){
        if(v == null)
            return;
        if(selectView != null)
            selectView.setTextColor(Color.parseColor("#999999"));
        selectView = (TextView)v;
        selectView.setTextColor(Color.parseColor("#1E90FF"));
    }

    class CityAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mCityStrings.size();
        }

        @Override
        public Object getItem(int position) {
            return mCityStrings.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HondlerClass hondlerClass = null;
            if(convertView == null) {
                hondlerClass = new HondlerClass();
                convertView = LayoutInflater.from(CityMianActivity.this).inflate(R.layout.city_item_layout, null);

                hondlerClass.text = (TextView) convertView.findViewById(R.id.text);
                hondlerClass.gridLayout = (GridLayout) convertView.findViewById(R.id.city_grid);
                convertView.setTag(hondlerClass);
            }else{
                hondlerClass = (HondlerClass)convertView.getTag();
            }
            String textString = mCityStrings.get(position);
            hondlerClass.text.setText(textString);

            List<String> citys = mCityDatas.get(textString);
            hondlerClass.gridLayout.removeAllViews();
            for(int i = 0; i < citys.size(); i++) {
                View itemView = LayoutInflater.from(CityMianActivity.this).
                        inflate(R.layout.city_grid_item, null);
                TextView itemCityTextView = (TextView) itemView.findViewById(R.id.text);
                itemCityTextView.setText(citys.get(i));
                itemCityTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String cityName = ((TextView)v).getText().toString();
                        if(cityName == null || cityName.equals(selectCityName)){
                            setResult(0);
                            finish();
                        }

                        Intent intent = new Intent();
                        intent.putExtra("city_name", cityName);
                        setResult(1, intent);
                        finish();
                    }
                });
                hondlerClass.gridLayout.addView(itemView);
            }
            return convertView;
        }

        class HondlerClass{
            public TextView text;
            public GridLayout gridLayout;
        }
    }

    private void updateCityDatas(String[] allCitys){
        for(int i = 0; i < allCitys.length; i++){
            String pinyin = CharacterParser.getInstance().getSelling(allCitys[i]);
            String sortString = pinyin.substring(0, 1).toUpperCase();

            List<String> citys = null;
            if(mCityStrings.contains(sortString)){
                citys = mCityDatas.get(sortString);
                citys.add(allCitys[i]);
            }else{
                citys = new ArrayList<String>();
                citys.add(allCitys[i]);
                mCityStrings.add(sortString);
                mCityDatas.put(sortString, citys);
            }
        }
        Collections.sort(mCityStrings, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                    return o1.compareTo(o2);
            }
        });
    }
}
